package reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;
import reggie.common.R;
import reggie.dto.SetmealDto;
import reggie.entity.Dish;
import reggie.entity.Setmeal;
import reggie.entity.SetmealDish;
import reggie.service.DishService;
import reggie.service.SetmealDishService;
import reggie.service.SetmealService;

import java.util.List;

@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    @Autowired
    private SetmealDishService setmealDishService;

    @Autowired
    private DishService dishService;

    /**
     * 分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize,String name) {

        Page<SetmealDto> setmealDtoPage = setmealService.pageWithSetmealDishes(page, pageSize, name);

        return R.success(setmealDtoPage);
    }

    /**
     * 添加套餐，并将套餐菜品保存进setmael_dish表
     * @param setmealDto
     * @return
     */
    @PostMapping
    @CacheEvict(value = "setmealCache", allEntries = true)
    public R<String> save(@RequestBody SetmealDto setmealDto) {

        setmealService.saveWithSetmealDishes(setmealDto);

        return R.success("添加套餐成功！");
    }

    /**
     * 根据setmealId查询对应setmealDto
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<SetmealDto> getByIdWithSetmealDish(@PathVariable Long id) {

        SetmealDto setmealDto = setmealService.getByIdWithSetmealDish(id);

        return R.success(setmealDto);
    }

    /**
     * 修改套餐以及对应的套餐菜品
     * @param setmealDto
     * @return
     */
    @PutMapping
    public R<String> updateWithSetmealDish(@RequestBody SetmealDto setmealDto) {

        setmealService.updateWithSetmealDish(setmealDto);

        return R.success("修改成功！");
    }


    /**
     * 批量停售/起售
     * @param status
     * @param ids
     * @return
     */
    @PostMapping("/status/{status}")
    public R<String> updateStatus(@PathVariable int status,@RequestParam List<Long> ids) {
//        log.info("_______________________________________________________________________________");
//
//        log.info(String.valueOf(status),ids);

        if (!ids.isEmpty()) {
            for (int i = 0; i < ids.size(); i++) {
                Long id = ids.get(i);

                UpdateWrapper<Setmeal> updateWrapper = new UpdateWrapper<>();
                updateWrapper.eq("id",id);
                updateWrapper.set("status",status);

                setmealService.update(updateWrapper);
            }
        }

        return R.success("操作成功！");

    }

    /**
     * 批量/单个删除菜品以及包含的口味信息
     * @param ids
     * @return
     */
    @DeleteMapping
    @CacheEvict(value = "setmealCache", allEntries = true)
    public R<String> delete(@RequestParam List<Long> ids) {
//        if (!ids.isEmpty()) {
//            for (int i = 0; i < ids.size(); i++) {
//                Long id = ids.get(i);
//                dishService.removeById(id);
//            }
//        }

        setmealService.deleteWithSetmealDish(ids);

        return R.success("删除成功！");
    }

    /**
     * 根据条件查询套餐信息
     * @param setmeal
     * @return
     */
    @GetMapping("/list")
    @Cacheable(value = "setmealCache", key = "#setmeal.categoryId + '_' + #setmeal.status")
    public R<List<Setmeal>> list(Setmeal setmeal) {

        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.eq(setmeal.getCategoryId() != null, Setmeal::getCategoryId,setmeal.getCategoryId());
        queryWrapper.eq(Setmeal::getStatus,setmeal.getStatus());
        queryWrapper.orderByDesc(Setmeal::getCreateTime);

        List<Setmeal> setmeals = setmealService.list(queryWrapper);

        return R.success(setmeals);

    }

    @GetMapping("/dish/{id}")
    public R<List<SetmealDish>> dishes(@PathVariable("id") String id) {

        List<SetmealDish> setmealDishes = null;


        //条件查询
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId,id);

        setmealDishes = setmealDishService.list(queryWrapper);

//        for (int i = 0; i < setmealDishes.size(); i++) {
//            SetmealDish setmealDish = setmealDishes.get(i);
//
//            System.out.println("------------------------------------------------------");
//            System.out.println(setmealDish);
//            Long dishId = setmealDish.getDishId();
//
//            Dish byId = dishService.getById(dishId);
//
//            System.out.println("------------------------------------------------------");
//            System.out.println(byId);
//
//            setmealDto.setDescription(byId.getDescription());
//            setmealDto.setDishImage(byId.getImage());
//
//            setmealDtoList.add(setmealDto);
//        }

        return R.success(setmealDishes);

    }
}
